Plex.Lib.JS.load('Data.Jax');
Plex.Lib.JS.load('Data.Global');

Global.create("Page");
Global.Page.create("Title");
Global.Page.create("Button_Title");
Global.Page.create("File_Content_ID");

Plex.Com.Page = {
	loaded : (new Array()),

	changeTitle : function(pageInfo)
	{
		var globName = Global.glob(pageInfo.language_ID,pageInfo.page_ID);
		var newTitle = prompt("Change Title To",Global.Page.Title.get(globName));

		if(newTitle)
		Global.Page.Title.set(globName,newTitle);
	},

	changeButtonTitle : function(pageInfo)
	{
		console.log(pageInfo)
		
		var globName = Global.glob(pageInfo.language_ID,pageInfo.page_ID);
		var newTitle = prompt("Change Button Title To",Global.Page.Button_Title.get(globName));

		if(newTitle)
		Global.Page.Button_Title.set(globName,newTitle);
	},

	register : function(object)
	{
		var pageInfo = object.info;
		
		// =============
		// = Functions =
		// =============
		
		object.changeTitle=function(){
			Plex.Com.Page.changeTitle(object.info);
		}
		
		object.changeButtonTitle=function(){
			Plex.Com.Page.changeButtonTitle(object.info);			
		}
		
		object.changeButtonImage=function(){
			Plex.Com.Page.changeButtonImage(object.info);			
		}
		
		// ===========
		// = Globals =
		// ===========

		var globName = Global.glob(pageInfo.language_ID,pageInfo.page_ID);

		Global.Page.Title.addWatch(globName,function(value){
			object.page.setTitle(value);
		});

		Global.Page.Title.set(globName,pageInfo.page_title);
		Global.Page.Button_Title.set(globName,pageInfo.button_title);
		Global.Page.File_Content_ID.set(globName,pageInfo.file_content_ID);

		// ================
		// = Context Menu =
		// ================
		
		object.page.context.addBreak(true);
		object.page.context.addOption("Change Button Image", object.changeButtonImage,"image",true);
		object.page.context.addOption("Change Button Title", object.changeButtonTitle,"text_allcaps",true);
		object.page.context.addBreak(true);
		object.page.context.addOption("Change Title", object.changeTitle,"text_allcaps",true);

		// ===========
		// = Toolbar =
		// ===========

		object.toolbar = new Plex.UI.Input.Toolbar(pageInfo.page_title);

		object.open = object.page.open;
		object.close = object.page.close;

		object.page.onOpen = object.toolbar.open;
		object.page.onClose = object.toolbar.close;


		object.toolbar.addButton("Close Page", object.close);
		object.toolbar.addSeperator();

		object.toolbar.addButton("Change Title", object.changeTitle);
		object.toolbar.addSeperator();

		object.toolbar.addButton("Change Button Title", object.changeButtonTitle);
		object.toolbar.addButton("Change Button Image", object.changeButtonImage);
		
		
		Global.Page.Title.addWatch(globName,function(value){
			object.toolbar.setTitle(value);
		});
	},

	load : function(pageId)
	{
		var self = this;

		this.Jax = new Plex.Data.Jax();
		this.Jax.feed='Page';
		this.Jax.action='Search';
		this.Jax.Var('language_ID',Plex.Settings.languageId);

		if(pageId == 0) //Zero equals homepage
		this.Jax.Var('homepage',1);
		else
		this.Jax.Var('page_ID',pageId);

		this.Jax.handler=function(feed)
		{
			var pageInfo = feed.getElementsByTagName('results').item(0).childNodes[0];
			var pageInfo = Plex.Data.XT.getAttributes(pageInfo);

			if(Plex.Com.Module[pageInfo.module_ID])
			{
				Plex.Com.Page.loaded[pageInfo.page_ID] = new Plex.Com.Module[pageInfo.module_ID](pageInfo);
//				Plex.Com.Page.loaded[pageInfo.page_ID].open();
			}
			else
			{
				Plex.Debug("Can't open page! Module ("+self.moduleId+") Not Loaded");
			}
		}

		this.Jax.Call();
	}
}

//var pg = new Plex.Com.Page.load(0);